package com.cardbaobao.cardbabyclient.ui.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.model.LoanPopFilterList;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;

import java.util.List;

/**
 * Created by Jiangzx on 9:28.
 * 贷款身份 身份不限、上班族。。。
 */

public class LoanProfessionPop extends PopupWindow {
    private LoanProfessionEnsuredListener loanProfessionEnsuredListener;
    public void setLoanProfessionEnsuredListener(LoanProfessionEnsuredListener listener){
        this.loanProfessionEnsuredListener=listener;
    }
    public interface LoanProfessionEnsuredListener {
        void onLoanProfessionEnsured(LoanPopFilterList.LoanPopFilter itemBean);
    }

    private Context context;
    private List<LoanPopFilterList.LoanPopFilter> data;
    private TextView tvReset;//重置
    private TextView tvEnsure;//确认
    private LoanProfessionAdapter adapter;
    private int selectedPosition=0;
    public LoanProfessionPop(Context context,@NonNull List<LoanPopFilterList.LoanPopFilter> data){
        this.context=context;
        this.data=data;
        View rootView= LayoutInflater.from(context).
                inflate(R.layout.pop_loan_profession,null);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvReset= (TextView) rootView.findViewById(R.id.tv_reset);
        tvEnsure= (TextView) rootView.findViewById(R.id.tv_ensure);
        initListener();
        GridView gridView= (GridView) rootView.findViewById(R.id.grid);
        gridView.setNumColumns(4);
        adapter=new LoanProfessionAdapter();
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelected(position);
            }
        });
        setAnimationStyle(R.style.CustomTopAnimateStyle);
        setBackgroundDrawable(new ColorDrawable());
        setOutsideTouchable(false);
        setWidth(ScreenUtil.getScreenWidth(context));
        //设置内容区域高度为屏幕高度的一半
        rootView.findViewById(R.id.pop_content).getLayoutParams().height=
                (int) (ScreenUtil.getScreenHeight(context)*0.5f);
        setContentView(rootView);
    }

    /**
     * 设置当前pop选中项
     * @param code 贷款身份code
     */
    public void setCurrentSelected(String code){
        int selected=0;//默认选中 身份不限
        for(int i=0,size=data.size();i<size;i++){
            if(data.get(i).getCode().equals(code)){
                selected=i;
                break;
            }
        }
        adapter.setSelected(selected);
    }

    private void initListener(){
        tvReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.setSelected(0);
                adapter.notifyDataSetChanged();
            }
        });
        tvEnsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if(loanProfessionEnsuredListener!=null){
                    selectedPosition=adapter.getSelected();
                    loanProfessionEnsuredListener.onLoanProfessionEnsured(data.get(selectedPosition));
                }
            }
        });
    }


    private class LoanProfessionAdapter extends BaseAdapter{
        private int selected=0;
        @Override
        public int getCount() {
            return data==null?0:data.size();
        }

        @Override
        public Object getItem(int position) {
            return data==null?null:data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if(convertView==null){
                convertView=LayoutInflater.from(context).
                        inflate(R.layout.item_card_screen_gv,parent,false);
                holder=new Holder();
                holder.rlScreen= (RelativeLayout) convertView.findViewById(R.id.rl_screen);
                holder.tvName= (TextView) convertView.findViewById(R.id.tv_name);
                holder.ivMark= (ImageView) convertView.findViewById(R.id.iv_choose);
                convertView.setTag(R.layout.item_card_screen_gv,holder);
            }else{
                holder= (Holder) convertView.getTag(R.layout.item_card_screen_gv);
            }
            holder.tvName.setText(data.get(position).getName());
            if(position==selected){
                holder.rlScreen.setBackgroundResource(R.drawable.tv_bg_line_ff9e27_34px);
                holder.tvName.setTextColor(0xffff9e27);
                holder.ivMark.setVisibility(View.VISIBLE);
            }else{
                holder.rlScreen.setBackgroundResource(R.drawable.tv_bg_line_eee_34px);
                holder.tvName.setTextColor(0xff222222);
                holder.ivMark.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

        private void setSelected(int selected){
            this.selected=selected;
            notifyDataSetChanged();
        }

        private int getSelected(){
            return this.selected;
        }

        private class Holder{
            private RelativeLayout rlScreen;
            private TextView tvName;
            private ImageView ivMark;
        }
    }
}
